#!/bin/sh
#
# http://security.stackexchange.com/questions/5158/for-luks-the-most-preferable-and-safest-cipher
#

. ../../func

VER=1.7.5
URL=https://www.kernel.org/pub/linux/utils/cryptsetup/v${VER%.*}
SRC=cryptsetup
PKG=cryptsetup_static
COMP=tar.xz
DESC="disk encryption support"
DEPS=
CAT=BuildingBlock
CWD=`pwd`

libuuid_ver=1.0.3  #2014-08-12
popt_ver=1.16      #May 07 2010
#lvm2_ver=2.02.116 #Jan 30 2015
#lvm2_ver=2.02.162 #Jul 28 2016
lvm2_ver=2.02.168  #Nov 30 2016
gpg_error_ver=1.24 #Jul 14 2016
gcrypt_ver=1.7.2   #Jul 14 2016

#-----------------------

which cc >/dev/null || ln -sv $(which gcc) /usr/bin/cc
export ac_cv_func_malloc_0_nonnull=yes
export ac_cv_func_realloc_0_nonnull=yes

build_libuuid() {
	extract libuuid-${libuuid_ver}.tar.gz
	cd libuuid-${libuuid_ver}
	opts="--prefix=$deps_dir"
	_configure
	_make
	_make install
	rv=$?
	cd ..
	return $rv
}

build_popt() {
	extract popt-${popt_ver}.tar.gz
	# popt has a very old config.sub
	cp -f ../config.sub popt-${popt_ver}
	cd popt-${popt_ver}
	opts="--prefix=$deps_dir"
	_configure
	_make
	_make install
	rv=$?
	cd ..
	return $rv
}

build_lvm2() {
	extract LVM2.${lvm2_ver}.tgz
	cd LVM2.${lvm2_ver}
	opts="--prefix=${deps_dir}
--disable-shared
--enable-static
--enable-static_link
--disable-selinux
--disable-udev-systemd-background-jobs
--with-user= --with-group=
--disable-nls
--disable-readline
--disable-blkid_wiping"
	# http://changetheworldwithyourpassion.blogspot.com/2009/09/fix-undefined-rplmalloc-on-autoconf.html
	export ac_cv_func_malloc_0_nonnull=yes
	_configure
	sed -i 's/SUBDIRS = .*/SUBDIRS = conf daemons include lib libdaemon libdm man/' Makefile
	_make LDFLAGS='-all-static' install_device-mapper #device-mapper
	cp libdm/ioctl/libdevmapper.a ${deps_dir}/lib/libdevmapper.a
	rv=$?
	cp libdm/libdevmapper.pc ${deps_dir}/lib/pkgconfig
	cp libdm/libdevmapper.h ${deps_dir}/include
	cd ..
	rm -f deps/lib/libdevmapper.so*
	return $rv
}

#-----------------------

build_libgpg_error() {
	extract libgpg-error-${gpg_error_ver}.tar.bz2
	cd libgpg-error-${gpg_error_ver}
	case $ARCH in
		i?86) SF='lock-obj-pub.i686-pc-linux-gnu.h' ;;
		x86_64) SF='lock-obj-pub.x86_64-pc-linux-musl.h' ;;
		#arm) SF='lock-obj-pub.armv5-unknown-linux-musleabi.h' ;;
		arm) SF='lock-obj-pub.armv6-unknown-linux-musleabihf.h' ;;
		aarch64) SF='lock-obj-pub.aarch64-unknown-linux-gnu.h' ;;
	esac
	if [ -f src/syscfg/$SF ] ; then
		ln -sfv $SF src/syscfg/lock-obj-pub.linux.h
	fi
	opts="--prefix=$deps_dir --disable-nls --enable-static --disable-shared --disable-doc"
	_configure
	_make
	_make install
	rv=$?
	cd ..
	return $rv
}

build_libgcrypt() {
	extract libgcrypt-${gcrypt_ver}.tar.gz
	cd libgcrypt-${gcrypt_ver}
	opts="
--prefix=${deps_dir}
--enable-static
--disable-shared
--disable-doc
--disable-asm
--disable-padlock-support
--disable-aesni-support
--disable-pclmul-support
--disable-sse41-support
--disable-drng-support
--disable-avx-support
--disable-avx2-support
--enable-ciphers=arcfour,blowfish,cast5,des,aes,twofish,serpent,rfc2268,seed,camellia,idea
"
	_configure
	_make
	_make install
	rv=$?
	cd ..
	return $rv
}

#-----------------------

build() {
	deps_dir=${PWD}/deps
	mkdir -p ${deps_dir}/lib/pkgconfig
	#--
	export PATH="${deps_dir}/bin:$PATH"
	export C_INCLUDE_PATH="${deps_dir}/include"
	export LD_LIBRARY_PATH="${deps_dir}/lib:$LD_LIBRARY_PATH"
	#--
	build_libgpg_error || exit 1
	build_libgcrypt || exit 1
	build_libuuid || exit 1
	build_popt || exit 1
	build_lvm2 || exit 1
	#--
	cd ${SRC}-${VER}
	make clean
	opts="--disable-nls
--disable-shared
--enable-static
--enable-static-cryptsetup
--disable-udev
--disable-selinux
--disable-veritysetup
--disable-python"
	export ac_cv_lib_uuid_uuid_clear=yes
	export ac_cv_lib_uuid_uuid_generate=yes
	export ac_cv_lib_popt_poptConfigFileToString=yes
	export ac_cv_lib_popt_poptGetContext=yes
	export ac_cv_lib_devmapper_dm_task_set_uuid=yes
	export DEVMAPPER_LIBS="-L${deps_dir}/lib/ -ldevmapper -lrt"
	export DEVMAPPER_CFLAGS="-I${deps_dir}/include"
	_configure
	sed -i 's|-ludev||' src/Makefile
	_make
	abort_if_file_not_found src/cryptsetup.static
	cp src/cryptsetup.static $SRC
	install -d -m 0755 $CWD/${PKG}-${VER}-${ARCH}/bin
	_install $SRC $CWD/${PKG}-${VER}-${ARCH}/bin
	cd -
	rm -rf deps libgcrypt* libgpg-error* libuuid-* LVM2* popt*
}

package() {
	# add this recipe
	install -d -m 0755 ./${PKG}-${VER}-${ARCH}/build
	cat ${PKG}.petbuild > ./${PKG}-${VER}-${ARCH}/build/$PKG-build-recipe
	# move, don't package
	cp -a --remove-destination ./${PKG}-${VER}-${ARCH}/* ../../00_${ARCH}
	rm -r ${SRC}-${VER}
	rm -r ${PKG}-${VER}-${ARCH}
	echo "moved to initrd"
	echo "done!"
}

#-----------------------

# main
retrieve https://sourceforge.net/projects/libuuid/files/libuuid-${libuuid_ver}.tar.gz
retrieve ftp://sources.redhat.com/pub/lvm2/LVM2.${lvm2_ver}.tgz
retrieve http://rpm5.org/files/popt/popt-${popt_ver}.tar.gz
retrieve ftp://ftp.gnupg.org/gcrypt/libgpg-error/libgpg-error-${gpg_error_ver}.tar.bz2
retrieve ftp://ftp.gnupg.org/gcrypt/libgcrypt/libgcrypt-${gcrypt_ver}.tar.gz
retrieve "${SRC}-${VER}.${COMP}"
[ "$DLD_ONLY" = "yes" ] && exit
extract ${SRC}-${VER}.${COMP}
build
package

